<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-cascader
          v-model="query.gradeName"
          placeholder="学校/年级"
          :options="options"
          :props="{ expandTrigger: 'hover' }"
          style="width: 185px;"
          class="filter-item"
          clearable
          @change="queryHandleChange"
        />
        <el-input v-model="query.name" clearable placeholder="班级名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="90px">
          <el-form-item label="学校/年级" prop="cascaderModelData">
            <el-cascader
              v-model="form.cascaderModelData"
              placeholder="学校/年级"
              :options="options"
              :props="{ expandTrigger: 'hover' }"
              style="width: 370px;"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item label="班级名称" prop="name">
            <el-input v-model="form.name" placeholder="班级名称" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="grade.school.name" label="学校名称" />
        <el-table-column prop="grade.name" label="年级名称" />
        <el-table-column prop="name" label="班级名称" />
        <el-table-column prop="gmtCreate" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gmtCreate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="gmtModified" label="修改时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gmtModified) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','class:edit','class:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudClass from '@/api/pay/class'
import crudSchool from '@/api/pay/school'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, grade: { id: null, name: null }, name: null, gmtCreate: null, gmtModified: null, cascaderModelData: [] }
export default {
  name: 'Class',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '班级', url: 'api/class', sort: 'id,desc', crudMethod: { ...crudClass }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'class:add'],
        edit: ['admin', 'class:edit'],
        del: ['admin', 'class:del']
      },
      rules: {
        name: [
          { required: true, message: '请输入班级', trigger: 'blur' }
        ],
        cascaderModelData: [
          { required: true, message: '请选择学校年级', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '班级名称' },
        { key: 'gradeId', display_name: '年级Id' },
        { key: 'gradeName', display_name: '年级名称' }
      ],
      options: []
    }
  },
  mounted() {
    crudSchool.query().then(res => {
      this.options = res.content
      this.optionHandle()
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToAdd]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit]() {
      this.form.cascaderModelData = [this.form.grade.school.id, this.form.grade.id]
      return true
    },
    handleChange(value) {
      this.form.grade.id = value[1]
    },
    queryHandleChange(value) {
      this.query.gradeName = value
      if (value.length === 0) {
        this.query.gradeId = null
      }
      if (value.length > 0) {
        this.query.gradeId = value[1]
      }
    },
    optionHandle() {
      this.options.forEach(school => {
        school.children = school.gradeList
        school.value = school.id
        school.label = school.name
        school.children.forEach(grade => {
          grade.value = grade.id
          grade.label = grade.name
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
